import tkinter
import tkmacosx
import time
from PIL import Image, ImageTk, ImageSequence
import pymysql
from tkinter.messagebox import *
# 导入算法 import DataSeq
import bubblesort
import bucketsort
import combsort
import cyclesort
import heapsort
import insertionsort
import mergesort
import monkeysort
import quicksort
import radixsort
import selectionsort
import shellsort
import pyperclip


def find_db(name, what):
    db = pymysql.connect(host='180.102.129.53', user='root', passwd='520hhy', db='hhy', charset='utf8')
    cursor = db.cursor()
    sql = """select {} from alg_info where name ='{}'""".format(what,name)
    cursor.execute(sql)
    res = cursor.fetchall()
    return res[0]



class ShowAlg:
    def __init__(self, alg_name):
        global root
        self.alg_name = alg_name
        self.frame = tkmacosx.SFrame(root,width=800, height=500)
        self.can = tkinter.Canvas(self.frame, height=350, width=350, bg="white")
        self.name = tkinter.Label(self.frame,text=self.alg_name, fg='black', font=("微软雅黑", 20, 'bold'), width=25)
        self.bt1 = tkmacosx.Button(self.frame, text="简介", width=100, height=50, command=lambda: self.change_area_text("info"))
        self.bt2 = tkmacosx.Button(self.frame, text="原理", width=100, height=50, command=lambda: self.change_area_text("Complexity"))
        self.bt3 = tkmacosx.Button(self.frame, text="时间复杂度", width=100, height=50, command=lambda: self.change_area_text("stable"))
        self.bt4 = tkmacosx.Button(self.frame, text="稳定性", width=100, height=50, command=lambda: self.change_area_text("code"))
        self.area = tkinter.Text(self.frame, height=15, width=55)
        self.show_alg()
        self.area.config(state="disable")

    def change_area_text(self, what):
        self.area.config(state="normal")
        res = find_db(self.alg_name, what)
        self.area.delete('1.0', 'end')
        self.area.insert("end", res[0])
        self.area.config(state="disable")
        # print(res[0])

    def show_alg(self):
        self.alg_label = tkinter.Label()
        self.alg_v = tkinter.Label(self.frame,text="排序速度")
        self.v = tkinter.StringVar()
        self.b1 = tkinter.Radiobutton(self.frame,text="快",variable=self.v, value="快")
        self.b2 = tkinter.Radiobutton(self.frame,text="中",variable=self.v, value="中")
        self.b3 = tkinter.Radiobutton(self.frame,text="慢",variable=self.v, value="慢")
        self.alg_l = tkinter.Label(self.frame,text="数组大小")
        self.l = tkinter.StringVar()
        self.ll1 = tkinter.Radiobutton(self.frame, text=10, variable=self.l, value="10")
        self.ll2 = tkinter.Radiobutton(self.frame, text=20, variable=self.l, value="20")
        self.ll3 = tkinter.Radiobutton(self.frame, text=30, variable=self.l, value="30")
        self.bttt= tkmacosx.Button(self.frame, text="点击生成算法动画", width=120, height=50,
                                   command=self.create_alg)
        self.cop = tkmacosx.Button(self.frame, text="一键复制", width=110, height=50,
                                    command=self.copy_text)

    # 写入剪贴板
    def copy_text(self):
        pyperclip.copy(self.area.get("1.0", "end")[:-1])

    def create_alg(self):
        dic = {"快":0, "慢":2, "中":0.1}
        if self.alg_name == "冒泡排序法":
            bubblesort.create_bubblesort("bullesort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="选择排序法":
            selectionsort.create_sort("bullesort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="猴子排序法":
            monkeysort.create_sort("monkeysort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="快速排序法":
            quicksort.create_sort("quicksort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="梳子排序法":
            combsort.create_sort("combsort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="归并排序法":
            mergesort.create_sort("mergesort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="圈排序":
            cyclesort.create_sort("cyclesort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="堆排序":
            heapsort.create_sort("heapsort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="插入排序":
            insertionsort.create_sort("insertionsort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        elif self.alg_name=="基数排序法":
            radixsort.create_sort("radixsort",float(dic[self.v.get()]),int(self.l.get()))
            showinfo("", "演示成功")
            return 0
        else:
            showinfo("", "未找到该算法")

    def grid(self):
        self.name.grid(row=0, column=0, columnspan=2)
        self.can.grid(row=1, column=0, rowspan=4)
        self.bt1.grid(row=1, column=1)
        self.bt2.grid(row=1, column=2)
        self.bt3.grid(row=1, column=3)
        self.bt4.grid(row=1, column=4)
        self.area.grid(row=2, column=1, columnspan=4)
        self.frame.grid(row=0,column=0)
        self.alg_v.grid(row=3,column=1)
        self.b1.grid(row=3,column=2)
        self.b2.grid(row=3,column=3)
        self.b3.grid(row=3,column=4)
        self.alg_l.grid(row=4, column=1)
        self.ll1.grid(row=4, column=2)
        self.ll2.grid(row=4, column=3)
        self.ll3.grid(row=4, column=4)
        self.bttt.grid(row=5,column=3)
        self.cop.grid(row=5,column=2)


def create_one(name, en_name):
    global root
    root = tkinter.Toplevel()
    root.title("算法可视化")
    root.geometry("800x500")
    c1 = ShowAlg(name)
    c1.grid()
    c1.change_area_text("info")
    # root.mainloop()
    path = "../img/"+en_name+".gif"
    while 1:
        im = Image.open(path)
        # GIF图片流的迭代器
        iter = ImageSequence.Iterator(im)
        # frame就是gif的每一帧，转换一下格式就能显示了
        for frame in iter:
            pic = ImageTk.PhotoImage(frame)
            c1.can.create_image((200, 200), image=pic)
            time.sleep(0.01)
            root.update_idletasks()  # 刷新
            root.update()


if __name__ == '__main__':
    create_one("冒泡排序法","BubbleSort")
